package cn.ibdsr.human.service.impl;

import cn.ibdsr.human.constant.IsDeleted;
import cn.ibdsr.human.constant.ServerInitEnum;
import cn.ibdsr.human.db.mapper.AdInfoMapper;
import cn.ibdsr.human.db.mapper.ServerInitMapper;
import cn.ibdsr.human.db.model.AdInfo;
import cn.ibdsr.human.db.model.ServerInit;
import cn.ibdsr.human.exception.BizExceptionEnum;
import cn.ibdsr.human.exception.BusinessException;
import cn.ibdsr.human.service.AdInforService;
import cn.ibdsr.human.transfer.ad.AdInforDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @description:
 * @author: cdl
 * @email: iamcimon@qq.com
 * @date: 2021/8/5 10:09
 */
@Service
@Slf4j
public class AdInforServiceImpl implements AdInforService {

    @Resource
    private AdInfoMapper adInfoMapper;

    @Resource
    private ServerInitMapper serverInitMapper;
    @Override
    public List<AdInforDTO> list() {
        List<AdInforDTO> adInforDTOS = adInfoMapper.listOrder();
        return adInforDTOS;
    }

    /**
     * 保存位置
     *
     * @param adInforDTOS
     * @return
     */
    @Transactional
    @Override
    public Boolean savePostion(Long userId, List<AdInforDTO> adInforDTOS) {
        for (AdInforDTO adInforDTO : adInforDTOS) {
            AdInfo adInfo = new AdInfo();
            adInfo.setId(adInforDTO.getId());
            adInfo.setSequence(adInforDTO.getSequence());
            adInfo.setModifiedUser(userId);
            adInfo.setModifiedTime(LocalDateTime.now());
            adInfo.updateById();
        }
        return null;
    }

    /**
     * 删除轮播图
     *
     * @param userId
     * @param id
     * @return
     */
    @Override
    public Long delete(Long userId, Long id) {

        AdInfo adInfo = adInfoMapper.selectById(id);
        if (adInfo == null) {
            throw new BusinessException(BizExceptionEnum.AD_NOT_EXIST);
        }
        if (adInfo.getIsDeleted().equals(IsDeleted.DELETED.getCode())) {
            return adInfo.getId();
        }
        adInfo.setModifiedTime(LocalDateTime.now());
        adInfo.setModifiedUser(userId);
        adInfo.setIsDeleted(IsDeleted.DELETED.getCode());
        adInfo.updateById();
        return adInfo.getId();
    }

    /**
     * @param userId
     * @param adInforDTO
     * @return
     */
    @Override
    public Long add(Long userId, AdInforDTO adInforDTO) {

        QueryWrapper<AdInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("is_deleted",IsDeleted.NORMAL.getCode());
        Integer count=adInfoMapper.selectCount(queryWrapper);

        QueryWrapper<ServerInit> serverInitQueryWrapper=new QueryWrapper<>();
        serverInitQueryWrapper.eq("type", ServerInitEnum.PICTURE_LENGTH.getType());
        ServerInit serverInit=serverInitMapper.selectOne(serverInitQueryWrapper);
        if(serverInit==null){
            throw new BusinessException(BizExceptionEnum.SERVIER_INIT_ERROR);
        }

        if(count>=Integer.valueOf(serverInit.getConfigValue())){
            throw new BusinessException(BizExceptionEnum.PICTURE_IS_FULL);
        }
        AdInfo adInfo = new AdInfo();
        Integer rank = adInfoMapper.getMaxSequence();
        if(rank==null) rank=0;
        rank+=1;
        adInfo.setImg(adInforDTO.getImg());
        adInfo.setName(adInforDTO.getName());
        adInfo.setSequence(rank);
        adInfoMapper.insert(adInfo);
        return adInfo.getId();
    }
}
